//
//  CalendarHelper.m
//  Common
//
//  Created by shiqing guo on 2018/11/27.
//  Copyright © 2018年 cloudstone. All rights reserved.
//

#import "CalendarHelper.h"
#import "EPColor.h"
#import "CommonHelper.h"
#import <EventKit/EventKit.h>

@implementation CalendarHelper
+ (CalendarHelper *)shared {
    static CalendarHelper *helper;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        helper = [[CalendarHelper alloc] init];
    });
    return helper;
}

+ (BOOL)authority
{
    EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
    if (EKAuthorizationStatusDenied == EKstatus ||
        EKAuthorizationStatusRestricted == EKstatus)
    {
        [CalendarHelper showAlertWithMessage:@"请在设备的 设置-隐私-日历 中打开。" title:@"未获得授权使用日历" cancel:^(UIAlertAction * _Nonnull action) {
            // do nothing ...
        } sure:^(UIAlertAction * _Nonnull action) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication] canOpenURL:url])
                [[UIApplication sharedApplication] openURL:url];
        }];
        return NO;
    }
    return YES;
}

+ (NSArray *)list
{
    [[CommonHelper shared].store reset];
    NSArray *list = [[CommonHelper shared].store calendarsForEntityType:EKEntityTypeEvent];
    return list;
}

+ (void)openAuthority:(void (^)(void))block
{
    EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
    if (EKAuthorizationStatusNotDetermined == status) {
        [[CommonHelper shared].store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
         {
             if (!granted)
             {
                 block();
             }
         }];
    }
}

#pragma mark - private
+ (void)showAlertWithMessage:(NSString *)message title:(NSString *)title cancel:(void (^ __nullable)(UIAlertAction *action))cancelHandler sure:(void (^ __nullable)(UIAlertAction *action))sureHandler
{
    [CalendarHelper showAlertWithMessage:message title:title cancelStr:@"取消" sureStr:@"确定" cancel:cancelHandler sure:sureHandler];
}

+ (void)showAlertWithMessage:(NSString *)message title:(NSString *)title cancelStr:(NSString *)cancelStr sureStr:(NSString *)sureStr cancel:(void (^ __nullable)(UIAlertAction *action))cancelHandler sure:(void (^ __nullable)(UIAlertAction *action))sureHandler
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelStr style:UIAlertActionStyleCancel handler:cancelHandler];
    [cancelAction setValue:[EPColor epColor_Blue] forKey:@"titleTextColor"];
    [alert addAction:cancelAction];
    
    UIAlertAction * surelAction = [UIAlertAction actionWithTitle:sureStr style:UIAlertActionStyleDefault handler:sureHandler];
    [surelAction setValue:[EPColor epColor_Blue] forKey:@"titleTextColor"];
    [alert addAction:surelAction];
    
    // 解决ipad闪退问题
    if ([alert respondsToSelector:@selector(popoverPresentationController)]) {
        alert.popoverPresentationController.sourceView = [CalendarHelper topViewController].view;
    }
    [[CalendarHelper topViewController] presentViewController:alert animated:YES completion:nil];
}

#pragma mark - 获取最顶层的控制器
+ (UIViewController *)topViewController
{
    return [CalendarHelper topViewControllerWithRootViewController:[[[UIApplication sharedApplication] delegate] window].rootViewController];
}

+ (UIViewController *)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return [CalendarHelper topViewControllerWithRootViewController:tabBarController.selectedViewController];
        
    }else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [CalendarHelper topViewControllerWithRootViewController:navigationController.visibleViewController];
        
    }else if (rootViewController.presentedViewController) {
        
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [CalendarHelper topViewControllerWithRootViewController:presentedViewController];
        
    }else {
        return rootViewController;
    }
}
@end
